/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import com.google.common.collect.Lists;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.syncinfo.ResourceSyncInfo;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

import java.util.List;
import java.util.Map;

/**
 * ClientUser for the 'p4 fstat' command.
 * 
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class Fstat extends AbstractPerforceCommand implements IPerforceCommand {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the result of the operation. */
  private List<ResourceSyncInfo> result = Lists.newArrayList();

  // --------------------------------------------------------------- Public
  // Static Factory Methods

  /**
   * Convenience method for creating instances of Fstat.
   * 
   * @param monitor for progress feedback.
   * 
   * @return the new Fstat instance.
   */
  public static final Fstat newInstance(IProgressMonitor monitor) {
    MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null);
    return new Fstat(status, monitor);
  }

  // ------------------------------------------------------------------------
  // Private Constructors

  /**
   * Creates a new instance.
   * 
   * @param status the MultiStatus object.
   * @param monitor for progress feedback.
   */
  private Fstat(MultiStatus status, IProgressMonitor monitor) {
    super(status);
  }

  // -------------------------------------------------------- Public Methods
  // (IClientUserDelegate)

  @Override
  public void outputStat(Map<String, ?> strDict) {
    getProgressMonitor().subTask(Helper.safeGetString(strDict, "depotFile")); //$NON-NLS-1$

    ResourceSyncInfo resourceSyncInfo = new ResourceSyncInfo();

    resourceSyncInfo.setAction(Helper.safeGetString(strDict, "action")); //$NON-NLS-1$
    resourceSyncInfo.setDepotFileName(Helper.safeGetString(strDict, "depotFile")); //$NON-NLS-1$
    resourceSyncInfo.setHeadAction(Helper.safeGetString(strDict, "headAction")); //$NON-NLS-1$
    resourceSyncInfo.setHeadType(Helper.safeGetString(strDict, "headType")); //$NON-NLS-1$
    resourceSyncInfo.setHeadTime(Helper.safeGetDate(strDict, "headTime")); //$NON-NLS-1$
    resourceSyncInfo.setHeadRev(Helper.safeGetInteger(strDict, "headRev").intValue()); //$NON-NLS-1$
    resourceSyncInfo.setHeadChange(Helper.safeGetInteger(strDict, "headChange").intValue()); //$NON-NLS-1$
    if (strDict.containsKey("change")) {
      resourceSyncInfo.setChange(Helper.safeGetChangelist(strDict, "change").intValue()); //$NON-NLS-1$
    }
    resourceSyncInfo.setHaveRev(Helper.safeGetInteger(strDict, "haveRev").intValue()); //$NON-NLS-1$
    resourceSyncInfo.setClientFileName(Helper.safeGetString(strDict, "clientFile")); //$NON-NLS-1$

    if (strDict.containsKey("unresolved")) //$NON-NLS-1$
    {
      resourceSyncInfo.setResolved(false);
    }

    // TODO: Store list with other opens
    if (strDict.containsKey("otherOpen0")) //$NON-NLS-1$
    {
      resourceSyncInfo.setOtherOpen(true);
    }

    if (strDict.containsKey("ourLock")) //$NON-NLS-1$
    {
      resourceSyncInfo.setHasOurLock(true);
    }

    result.add(resourceSyncInfo);

    getProgressMonitor().worked(1);
  }

  // ----------------------------------------------------------- Public Methods
  // (IPerforceCommand)

  public String getCommand() {
    return "fstat"; //$NON-NLS-1$
  }

  @Override
  public IStatus getStatus() {
    return getMultiStatus();
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  /**
   * Returns the result of the operation.
   * 
   * @return array of ResourceSyncInfo's.
   */
  public ResourceSyncInfo[] getResult() {
    return result.toArray(new ResourceSyncInfo[result.size()]);
  }
}
